<?php

namespace app\common\service\setting;
use app\common\cache\setting\SettingStoreTypeCache;
use app\common\model\setting\SettingStoreTypeModel;

/**
 * 店铺类目
 */
class SettingStoreTypeService
{
    /**
     * 添加、修改字段
     * @var array
     */
    public static $edit_field = [
            'id' => '',
        'code/s' => '',
        'title/s' => '',
        'is_disable/d' => 0,
        'image_id/d' => null,
        'color/s' => '',
        'sort/d' => 250,
        'remark' => '',
    ];
    /**
     * 店铺类目列表
     *
     * @param array  $where 条件
     * @param int    $page  页数
     * @param int    $limit 数量
     * @param array  $order 排序
     * @param string $field 字段
     *
     * @return array
     */
    public static function list($where = [], $page = 1, $limit = 10,  $order = [], $field = '')
    {
        $model = new SettingStoreTypeModel();
        $pk = $model->getPk();
        if (empty($field)) {
            $field = 'id,code,title,is_disable,create_time,update_time,image_id,color,sort,remark';
        }
        if (empty($order)) {
            $order = ['sort'=>'asc',$pk => 'desc'];
        }
        $with = $append = $hidden = [];
        if (strpos($field, 'image_id') !== false) {
            $with[]   = $hidden[] = 'image';
            $append[] = 'image_url';
        }
        if ($page == 0 || $limit == 0) {
            return $model->with($with)->append($append)->hidden($hidden)->field($field)->where($where)->order($order)->select()->toArray();
        }
        $count = $model->where($where)->count();
        $pages = ceil($count / $limit);
        $list = $model->with($with)->append($append)->hidden($hidden)->field($field)->where($where)->page($page)->limit($limit)->order($order)->select()->toArray();
        return compact('count', 'pages', 'page', 'limit', 'list');
    }
    /**
     * 查询进行缓存了的店铺类目
     * @Author: 易军辉
     * @DateTime:2024-10-18 14:53
     * @param $where 条件
     * @param $order 排序
     * @param $field 字段
     */
    public static function getList($where = [], $order = [], $field = '')
    {
        $key = where_cache_key('list',$where, $order, $field);
        $data = SettingStoreTypeCache::get($key);
        if (empty($data)) {
            $model = new SettingStoreTypeModel();
            $pk = $model->getPk();

            if (empty($field)) {
                $field = $pk . ',code,title,is_disable,create_time,update_time,image_id,color,sort,remark';
            }
            if (empty($order)) {
                $order = ['sort' => 'asc', $pk => 'desc'];
            }

            $with = $append = $hidden = [];
            if (strpos($field, 'image_id') !== false) {
                $with[]   = $hidden[] = 'image';
                $append[] = 'image_url';
            }
            $data = $model->field($field)->where($where)
                ->with($with)->append($append)->hidden($hidden)
                ->order($order)->select()->toArray();
            SettingStoreTypeCache::set($key, $data);
        }
        return $data;
    }
    /**
     * 店铺类目信息
     *
     * @param int  $id   店铺类目id
     * @param bool $exce 不存在是否抛出异常
     *
     * @return array|Exception
     */
    public static function info($id, $exce = true)
    {
        $info = SettingStoreTypeCache::get($id);
        if (empty($info)) {
            $model = new SettingStoreTypeModel();
            $info = $model->find($id);
            if (empty($info)) {
                if ($exce) {
                    exception('店铺类目不存在：' . $id);
                }
                return [];
            }
            $info = $info->append(['image_url'])->hidden(['image'])->toArray();
            SettingStoreTypeCache::set($id, $info);
        }
        return $info;
    }
    /**
     * 店铺类目添加
     *
     * @param array $param 店铺类目信息
     *
     * @return array|Exception
     */
    public static function add($param)
    {
        $model = new SettingStoreTypeModel();
        $pk = $model->getPk();
        unset($param[$pk]);
        $param['create_uid']  = user_id();
        $param['create_time'] = datetime();
        $model->save($param);
        $id = $model->$pk;
        if (empty($id)) {
            exception();
        }
        $param[$pk] = $id;
        SettingStoreTypeCache::clear();
        return $param;
    }
     /**
     * 店铺类目修改
     *
     * @param int|array $ids   店铺类目id
     * @param array     $param 店铺类目信息
     *
     * @return array|Exception
     */
    public static function edit($ids, $param = [])
    {
        $model = new SettingStoreTypeModel();
        $pk = $model->getPk();
        unset($param[$pk], $param['ids']);
        $param['update_uid']  = user_id();
        $param['update_time'] = datetime();
        $res = $model->where($pk, 'in', $ids)->update($param);
        if (empty($res)) {
            exception();
        }
        $param['ids'] = $ids;
        SettingStoreTypeCache::clear();
        return $param;
    }
    /**
     * 店铺类目删除
     *
     * @param array $ids  店铺类目id
     * @param bool  $real 是否真实删除
     *
     * @return array|Exception
     */
    public static function dele($ids, $real = false)
    {
        $model = new SettingStoreTypeModel();
        $pk = $model->getPk();
        if ($real) {
            $res = $model->where($pk, 'in', $ids)->delete();
        } else {
            $update = delete_update();
            $res = $model->where($pk, 'in', $ids)->update($update);
        }
        if (empty($res)) {
            exception();
        }
        $update['ids'] = $ids;
        SettingStoreTypeCache::clear();
        return $update;
    }
}
